package com.ruoyi.web.controller.api;

import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.estate.Notice;
import com.ruoyi.system.service.estate.INoticeService;
import com.ruoyi.common.annotation.Anonymous;

/**
 * 公告信息API Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/api/notice")
public class NoticeApiController extends BaseController {
    
    @Autowired
    private INoticeService noticeService;
    
    /**
     * 获取公告列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Notice notice) {
        startPage();
        // 设置查询条件：状态正常
        notice.setStatus("0");
        List<Notice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }
    
    /**
     * 获取公告详情
     */
    @Anonymous
    @GetMapping("/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId) {
        return AjaxResult.success(noticeService.selectNoticeById(noticeId));
    }
    
    /**
     * 获取置顶公告列表
     */
    @Anonymous
    @GetMapping("/top")
    public AjaxResult getTopList() {
        List<Notice> resultList = new ArrayList<>();
        
        // 首先获取置顶的公告
        Notice topNotice = new Notice();
        topNotice.setStatus("0");
        topNotice.setIsTop("1");
        List<Notice> topList = noticeService.selectNoticeList(topNotice);
        if (topList != null && !topList.isEmpty()) {
            resultList.addAll(topList);
        }
        
        // 如果置顶公告不足3条，再获取最新的公告补充
        if (resultList.size() < 3) {
            Notice latestNotice = new Notice();
            latestNotice.setStatus("0");
            // 按创建时间倒序排序，获取最新的公告
            List<Notice> latestList = noticeService.selectNoticeList(latestNotice);
            if (latestList != null && !latestList.isEmpty()) {
                // 过滤掉已经添加的置顶公告
                for (Notice notice : latestList) {
                    boolean exists = false;
                    for (Notice existingNotice : resultList) {
                        if (existingNotice.getNoticeId().equals(notice.getNoticeId())) {
                            exists = true;
                            break;
                        }
                    }
                    if (!exists) {
                        resultList.add(notice);
                        if (resultList.size() >= 3) {
                            break;
                        }
                    }
                }
            }
        }
        
        return AjaxResult.success(resultList);
    }
} 